package com.ffcs.web.controllers;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ffcs.web.model.dto.BaseResponse;
import com.ffcs.web.model.dto.FreeAskResponse;
import com.ffcs.web.model.entity.Counselor;
import com.ffcs.web.model.entity.FreeAsk;
import com.ffcs.web.model.entity.WxUser;
import com.ffcs.web.services.CounselorService;
import com.ffcs.web.services.FreeAskService;
import com.ffcs.web.services.MessageService;
import com.ffcs.web.services.WxUserService;
import com.ffcs.web.utils.CommonConfig;
import com.ffcs.web.utils.JsSign;

@Controller
public class FreeAskController {

	private Logger logger = Logger.getLogger(FreeAskController.class);
	@Autowired
	private FreeAskService freeAskService;
	@Autowired
	private WxUserService wxUserService;
	@Autowired
	private CounselorService counselorService;
	@Autowired
	private MessageService messageService;
	
	/**
	 * 删除我的提问
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("deleteFreeAsk")
	public void deleteFreeAsk(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		String deleteId=request.getParameter("deleteId");
		freeAskService.deleteFreeAsk(deleteId);
		response.sendRedirect("myAsk.jsp");
	}
	
	/**
	 * 提交问题
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("gotoMyQuestion")
	public void gotoMyQuestion(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		String direction=request.getParameter("direction");
		String question=request.getParameter("question");
		String type=request.getParameter("type");
		WxUser wxUser=(WxUser)request.getSession().getAttribute("wxUser");
		FreeAsk freeAsk=freeAskService.addFreeAsk(wxUser,direction,question,type);
		if(type.equals("1")){
			String newMp3Name=freeAskService.main1(JsSign.getAccessToken(),question,CommonConfig.getProperty("YUYIN_ADRESS"));
			freeAsk.setIsLoad((short)1);
			freeAsk.setQuestion(newMp3Name);
			freeAskService.saveFreeAsk(freeAsk);
			System.out.println("id="+freeAsk.getId()+"微信语音已成功下载,文件名:"+newMp3Name);
		}
		
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
		String content="(免费提问待回复)<br/> 您好，有一个在"+sdf.format(freeAsk.getAskTime())+"的提问正等待您的回复!";
		
		if(!freeAsk.getAskCounseTel().equals("123456789")){
			Counselor counselor=counselorService.getCounselor(freeAsk.getAskCounseTel());
			messageService.inserSystemMessage(counselor.getId(),"1",content);
		}
		response.sendRedirect("myAsk.jsp");
	}
	
	/**
	 * 我的提问
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("getMyFreeAsks")
	@ResponseBody
	public FreeAskResponse getMyFreeAsks(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		FreeAskResponse freeAskResponse=new FreeAskResponse();
		WxUser wxUser=(WxUser)request.getSession().getAttribute("wxUser");
		List<FreeAsk> freeAskList=freeAskService.getFreeAsk(wxUser.getId());
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for (FreeAsk freeAsk : freeAskList) {
			freeAsk.setAskTimeStr(sdf.format(freeAsk.getAskTime()));
			if(freeAsk.getType()==1){
				freeAsk.setQuestion(CommonConfig.getProperty("YUYIN_URL_FRONT")+freeAsk.getQuestion());
			}
		}
		freeAskResponse.setResultCode("0");
		freeAskResponse.setResultDesc("success");
		freeAskResponse.setFreeAskList(freeAskList);
		return freeAskResponse;
	}
	
	/**
	 * 获取免费提问未被解答的列表
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("getFreeAskList")
	@ResponseBody
	public FreeAskResponse getFreeAskList(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		FreeAskResponse freeAskResponse=new FreeAskResponse();
		
		if(null==request.getSession().getAttribute("counseTel") || "".equals((String) request.getSession().getAttribute("counseTel"))){
			freeAskResponse.setResultCode("-2");
			freeAskResponse.setResultDesc("登录超时");
			return freeAskResponse;
		}
		
		String counseTel=(String) request.getSession().getAttribute("counseTel");
		List<FreeAsk> freeAskList=freeAskService.getFreeAskList(counseTel);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for (FreeAsk freeAsk : freeAskList) {
			freeAsk.setAskTimeStr(sdf.format(freeAsk.getAskTime()));
			if(freeAsk.getType()==1){
				freeAsk.setQuestion(CommonConfig.getProperty("YUYIN_URL_FRONT")+freeAsk.getQuestion());
			}
		}
		freeAskResponse.setResultCode("0");
		freeAskResponse.setResultDesc("success");
		freeAskResponse.setFreeAskList(freeAskList);
		return freeAskResponse;
	}
	
	/**
	 * 去我的回复页面
	 * @param request
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("gotoHuifuAsk")
	public void gotoHuifuAsk(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		String askId=request.getParameter("askId");
		FreeAsk freeAsk=freeAskService.getFreeAskDesc(Integer.parseInt(askId));
		
		WxUser wxUser=wxUserService.getUserByUserId(freeAsk.getUserId());
		request.setAttribute("wxUser",wxUser);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd");
		freeAsk.setAskTimeStr(sdf.format(freeAsk.getAskTime()));
		if(null!=freeAsk.getPingjia() && !"".equals(freeAsk.getPingjia())){
			freeAsk.setPingjiaTimeStr(sdf2.format(freeAsk.getPingjiaTime()));
		}
		if(freeAsk.getType()==1){
			freeAsk.setQuestion(CommonConfig.getProperty("YUYIN_URL_FRONT")+freeAsk.getQuestion());
		}
		if(null!=freeAsk.getCounseAnswer() && freeAsk.getCounseAnswer().contains(".mp3")){
			request.setAttribute("isYuyin",1);
			freeAsk.setCounseAnswer(CommonConfig.getProperty("YUYIN_URL_FRONT")+freeAsk.getCounseAnswer());
		}else{
			request.setAttribute("isYuyin",0);
		}
		
		request.setAttribute("freeAsk",freeAsk);
		if(freeAsk.getStatus()==0){
			request.getRequestDispatcher("business/24.jsp").forward(request, response);
		}else if(freeAsk.getStatus()==1){
			request.getRequestDispatcher("business/23.jsp").forward(request, response);
		}else{
			request.getRequestDispatcher("business/23.jsp").forward(request, response);
		}
	}
	
	@RequestMapping("AskHuifu")
	public void AskHuifu(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		String askId=request.getParameter("askId");
		String question=request.getParameter("question");
		String type=request.getParameter("type");
		FreeAsk freeAsk=freeAskService.updateFreeAsk(askId,question);
		if(type.equals("1")){
			String newMp3Name=freeAskService.main1(JsSign.getAccessToken(),question,CommonConfig.getProperty("YUYIN_ADRESS"));
			freeAsk.setCounseAnswer(newMp3Name);
			freeAskService.saveFreeAsk(freeAsk);
			System.out.println("咨询师回复--》id="+freeAsk.getId()+"微信语音已成功下载,文件名:"+newMp3Name);
		}
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
		String content="(提问被回复)<br/> 您好，您在"+sdf.format(freeAsk.getAskTime())+"的提问已被"+freeAsk.getAskCounseTel()+"回复!";
		messageService.inserSystemMessage(freeAsk.getUserId(),"2",content);
		response.sendRedirect("business/myFreeAsks.jsp");
	}
	
	/**
	 * 评价接口
	 * @param response
	 * @param askId
	 * @param pingjiaxx
	 * @param pingjia
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("gotoPingjiaAsk")
	public void gotoPingjiaAsk(HttpServletResponse response, String askId,String pingjiaxx,String pingjia) throws ServletException, IOException{
		freeAskService.gotoPingjiaAsk(askId,pingjiaxx,pingjia);
		response.sendRedirect("gotoMyAskAnswer?askId="+askId);
	}
	
	@RequestMapping("gotoMyAskAnswer")
	public void gotoMyAskAnswer(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		String askId=request.getParameter("askId");
		FreeAsk freeAsk=freeAskService.getFreeAskDesc(Integer.parseInt(askId));
		
		Counselor counselor=counselorService.getCounselor(freeAsk.getAskCounseTel());
		request.setAttribute("counselor",counselor);
		WxUser wxUser=wxUserService.getUserByUserId(freeAsk.getUserId());
		request.setAttribute("wxUser",wxUser);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd");
		freeAsk.setAskTimeStr(sdf.format(freeAsk.getAskTime()));
		if(null!=freeAsk.getPingjia() && !"".equals(freeAsk.getPingjia())){
			freeAsk.setPingjiaTimeStr(sdf2.format(freeAsk.getPingjiaTime()));
		}
		if(freeAsk.getType()==1){
			freeAsk.setQuestion(CommonConfig.getProperty("YUYIN_URL_FRONT")+freeAsk.getQuestion());
		}
		if(null!=freeAsk.getCounseAnswer() && freeAsk.getCounseAnswer().contains(".mp3")){
			request.setAttribute("isYuyin",1);
			freeAsk.setCounseAnswer(CommonConfig.getProperty("YUYIN_URL_FRONT")+freeAsk.getCounseAnswer());
		}else{
			request.setAttribute("isYuyin",0);
		}
		
		request.setAttribute("freeAsk",freeAsk);
		request.getRequestDispatcher("myAskAnswer.jsp").forward(request, response);
	}
}
